<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
	
	$lang = $_SESSION['lang'];
	if (empty($lang) || $lang == "EN") {
		include "../value/value_en.php";
	} else if($lang == "VN") {
		include "../value/value_vn.php";
	}
	else {
		include "../value/value_kr.php";
	}
function ViPAencode($text) {
    $origin = array(
        '/([0-9])/',
        '/a/', '/à/', '/ả/', '/ã/', '/á/', '/ạ/',
        '/ă/', '/ằ/', '/ẳ/', '/ẵ/', '/ắ/', '/ặ/',
        '/â/', '/ầ/', '/ẩ/', '/ẫ/', '/ấ/', '/ậ/',
        '/e/', '/è/', '/ẻ/', '/ẽ/', '/é/', '/ẹ/',
        '/ê/', '/ề/', '/ể/', '/ễ/', '/ế/', '/ệ/',
        '/i/', '/ì/', '/ỉ/', '/ĩ/', '/í/', '/ị/',
        '/o/', '/ò/', '/ỏ/', '/õ/', '/ó/', '/ọ/',
        '/ô/', '/ồ/', '/ổ/', '/ỗ/', '/ố/', '/ộ/',
        '/ơ/', '/ờ/', '/ở/', '/ỡ/', '/ớ/', '/ợ/',
        '/u/', '/ù/', '/ủ/', '/ũ/', '/ú/', '/ụ/',
        '/ư/', '/ừ/', '/ử/', '/ữ/', '/ứ/', '/ự/',
        '/y/', '/ỳ/', '/ỷ/', '/ỹ/', '/ý/', '/ỵ/',
        '/A/', '/À/', '/Ả/', '/Ã/', '/Á/', '/Ạ/',
        '/Ă/', '/Ằ/', '/Ẳ/', '/Ẵ/', '/Ắ/', '/Ặ/',
        '/Â/', '/Ầ/', '/Ẩ/', '/Ẫ/', '/Ấ/', '/Ậ/',
        '/E/', '/È/', '/Ẻ/', '/Ẽ/', '/É/', '/Ẹ/',
        '/Ê/', '/Ề/', '/Ể/', '/Ễ/', '/Ế/', '/Ệ/',
        '/I/', '/Ì/', '/Ỉ/', '/Ĩ/', '/Í/', '/Ị/',
        '/O/', '/Ò/', '/Ỏ/', '/Õ/', '/Ó/', '/Ọ/',
        '/Ô/', '/Ồ/', '/Ổ/', '/Ỗ/', '/Ố/', '/Ộ/',
        '/Ơ/', '/Ờ/', '/Ở/', '/Ỡ/', '/Ớ/', '/Ợ/',
        '/U/', '/Ù/', '/Ủ/', '/Ũ/', '/Ú/', '/Ụ/',
        '/Ư/', '/Ừ/', '/Ử/', '/Ữ/', '/Ứ/', '/Ự/',
        '/Y/', '/Ỳ/', '/Ỷ/', '/Ỹ/', '/Ý/', '/Ỵ/',
        '/d/', '/đ/', '/D/', '/Đ/');

    $codes = array(
        "-$1",
        'a0', 'a02', 'a03', 'a04', 'a05', 'a06',
        'a1', 'a12', 'a13', 'a14', 'a15', 'a16',
        'a2', 'a22', 'a23', 'a24', 'a25', 'a26',
        'e0', 'e02', 'e03', 'e04', 'e05', 'e06',
        'e1', 'e12', 'e13', 'e14', 'e15', 'e16',
        'i0', 'i02', 'i03', 'i04', 'i05', 'i06',
        'o0', 'o02', 'o03', 'o04', 'o05', 'o06',
        'o1', 'o12', 'o13', 'o14', 'o15', 'o16',
        'o2', 'o22', 'o23', 'o24', 'o25', 'o26',
        'u0', 'u02', 'u03', 'u04', 'u05', 'u06',
        'u1', 'u12', 'u13', 'u14', 'u15', 'u16',
        'y0', 'y02', 'y03', 'y04', 'y05', 'y06',
        'A0', 'A02', 'A03', 'A04', 'A05', 'A06',
        'A1', 'A12', 'A13', 'A14', 'A15', 'A16',
        'A2', 'A22', 'A23', 'A24', 'A25', 'A26',
        'E0', 'E02', 'E03', 'E04', 'E05', 'E06',
        'E1', 'E12', 'E13', 'E14', 'E15', 'E16',
        'I0', 'I02', 'I03', 'I04', 'I05', 'I06',
        'O0', 'O02', 'O03', 'O04', 'O05', 'O06',
        'O1', 'O12', 'O13', 'O14', 'O15', 'O16',
        'O2', 'O22', 'O23', 'O24', 'O25', 'O26',
        'U0', 'U02', 'U03', 'U04', 'U05', 'U06',
        'U1', 'U12', 'U13', 'U14', 'U15', 'U16',
        'Y0', 'Y02', 'Y03', 'Y04', 'Y05', 'Y06',
        'd81', 'd91', 'D81', 'D91');

    $encoded = preg_replace($origin, $codes, $text);
    return $encoded;
}
	
function ViSort($list) { // $list must be an array
    for ($i = 0; $i < count($list); $i++) {
        $list_encoded[$i] = ViPAencode($list[$i]);
        $list_encoded[$i] = preg_replace("/\b(\w+)([012])([2-6])(\w+)?\b/", "$1$2$4$3", $list_encoded[$i]);
    }
    // Convert to lowercase
    $list_encoded_lowercase = array_map('strtolower', $list_encoded);
    // Sort list by encoded list (in lowercase),
    // so we don't have to write the crazy function called 'ViPAdecode' :p 
    array_multisort($list_encoded_lowercase, SORT_ASC, SORT_STRING, $list);

    return $list;
}
function ViSortByName($list) { // $list must be an array
// TODO: The priority must be checked again!
// There are some related articles on Journal of Language
// (printed about 5 years ago)...
    for ($i = 0; $i < count($list); $i++) {
        $list_reversed = explode(" ", trim($list[$i]));
        $list_reversed = array_reverse($list_reversed);
        $list[$i] = implode(" ", $list_reversed);
    }

    $list_sorted = ViSort($list);

    for ($j = 0; $j < count($list_sorted); $j++) {
        $list_unreversed = explode(" ", $list_sorted[$j]);
        $list_unreversed = array_reverse($list_unreversed);
        $list_sorted[$j] = implode(" ", $list_unreversed);
    }

    return $list_sorted;
}
$employees=$_SESSION['employeeworking'];
$mangemployees=$_SESSION['mangemployeeworking'];



	$mang=array("A","B","C","D","E","F","G","H","I","J","K","L");
	$mangshow=array();
	$i=0;
	if(isset($_REQUEST['check_no'])){
		$mangshow[$mang[$i]]=array(0,$l_no);
		$i++;
	}
	if(isset($_REQUEST['check_employee_name'])){
		$mangshow[$mang[$i]]=array(1,$l_employee_name);
		$i++;
	}
	if(isset($_REQUEST['check_birthday'])){
		$mangshow[$mang[$i]]=array(2,$l_birthday);
		$i++;
	}
	if(isset($_REQUEST['check_username'])){
		$mangshow[$mang[$i]]=array(3,$l_username);
		$i++;
	}
	if(isset($_REQUEST['check_sex'])){
		$mangshow[$mang[$i]]=array(4,$l_sex);
		$i++;
	}
	if(isset($_REQUEST['check_department'])){
		$mangshow[$mang[$i]]=array(5,$l_department);
		$i++;
	}
	if(isset($_REQUEST['check_position'])){
		$mangshow[$mang[$i]]=array(6,$l_position);
		$i++;
	}
	if(isset($_REQUEST['check_startwork'])){
		$mangshow[$mang[$i]]=array(7,$l_startwork);
		$i++;
	}
	if(isset($_REQUEST['check_cmnd'])){
		$mangshow[$mang[$i]]=array(8,$l_cmnd);
		$i++;
	}
	if(isset($_REQUEST['check_address'])){
		$mangshow[$mang[$i]]=array(9,$l_address);
		$i++;
	}
	if(isset($_REQUEST['check_mobile'])){
		$mangshow[$mang[$i]]=array(10,$l_mobile);
		$i++;
	}
	if(isset($_REQUEST['check_totaldays'])){
		$mangshow[$mang[$i]]=array(11,$l_totaldays);
		$i++;
	}
	
	
	//include '../Classes/PHPExcel.php';
	/** Include path **/
	set_include_path(get_include_path() . PATH_SEPARATOR . 'ClassesExcel/');
	
	/** PHPExcel */
	require_once 'PHPExcel.php';
	
	/** PHPExcel_RichText */
	require_once 'PHPExcel/RichText.php';
	
	
	$filename ="export_employee_working.xls";
	
	$objPHPExcel = new PHPExcel();

	
	$styleArray = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,
			'startcolor' => array(
				'argb' => 'FFA0A0A0',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	$styleArray1 = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray2 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray3 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray4 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray5 = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'font' => array(
			'bold' => true,
			
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);

	$objPHPExcel->setActiveSheetIndex(0);
	
	$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Times New Roman');
	$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
	$objPHPExcel->getActiveSheet()->mergeCells('A1:'.$mang[count($mangshow)-1].'1');
	$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->setCellValue('A1',$l_employeeworking);

	$objPHPExcel->getActiveSheet()->freezePane('A5');
	

	for($i=0;$i<count($mangshow);$i++){
		$objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(25);
		$objPHPExcel->getActiveSheet()->getStyle($mang[$i].'4')->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getColumnDimension($mang[$i])->setAutoSize(true);
		$objPHPExcel->getActiveSheet()->setCellValue($mang[$i].'4',$mangshow[$mang[$i]][1]);
	}
	
	/*$objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(25);
	$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('A4',$l_no);
	
	
	$objPHPExcel->getActiveSheet()->getStyle('B4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('B4', $l_employee_name);
	
	$objPHPExcel->getActiveSheet()->getStyle('C4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('C4', $l_birthday);
	
	$objPHPExcel->getActiveSheet()->getStyle('D4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('D4', $l_username);
	
	
	$objPHPExcel->getActiveSheet()->getStyle('E4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('E4', $l_sex);
	
	$objPHPExcel->getActiveSheet()->getStyle('F4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('F4', $l_department);

	$objPHPExcel->getActiveSheet()->getStyle('G4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('G4', $l_position);
	
	$objPHPExcel->getActiveSheet()->getStyle('H4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('H4', $l_startwork);
	
	$objPHPExcel->getActiveSheet()->getStyle('I4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('I4', $l_cmnd);
	
	$objPHPExcel->getActiveSheet()->getStyle('J4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('J4', $l_address);
	
	$objPHPExcel->getActiveSheet()->getStyle('K4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('K4', $l_mobile);
	
	$objPHPExcel->getActiveSheet()->getStyle('L4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('L4', $l_totaldays);*/
	
	
	
	
	$key=5;

           		$mangemployees = ViSortByName($mangemployees);
				for ($i = 0; $i < count($mangemployees); $i++) {
					$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(25);
					for($j=0;$j<count($mangshow);$j++){
						$objPHPExcel->getActiveSheet()->getStyle($mang[$j].$key)->applyFromArray($styleArray2);
						if($mangshow[$mang[$j]][0]==0)
							$objPHPExcel->getActiveSheet()->setCellValue($mang[$j].$key,$i+1);
						else if($mangshow[$mang[$j]][0]==11){
							$date1 = $employees[$mangemployees[$i]][0][7];
						    $date2 = date("Y-m-d");
						
						    $diff = abs(strtotime($date2) - strtotime($date1));
						
						    $years = floor($diff / (365 * 60 * 60 * 24));
						    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
						    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
							$objPHPExcel->getActiveSheet()->setCellValue($mang[$j].$key,  $years.' '.$l_year.','.$months.' '. $l_month.','.$days.' '.$l_date);
						}
						else
						$objPHPExcel->getActiveSheet()->setCellValue($mang[$j].$key,$employees[$mangemployees[$i]][0][$mangshow[$mang[$j]][0]]);
						
					}
           					/*$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(25);
							$objPHPExcel->getActiveSheet()->getStyle('A'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('A'.$key, $i+1);
							
							$objPHPExcel->getActiveSheet()->getStyle('B'.$key)->applyFromArray($styleArray3);
							$objPHPExcel->getActiveSheet()->setCellValue('B'.$key,  $employees[$mangemployees[$i]][0][1]);
							
							$objPHPExcel->getActiveSheet()->getStyle('C'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('C'.$key,  $employees[$mangemployees[$i]][0][2]);
							
							$objPHPExcel->getActiveSheet()->getStyle('D'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('D'.$key,  $employees[$mangemployees[$i]][0][3]);
							
							
							$objPHPExcel->getActiveSheet()->getStyle('E'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('E'.$key, $employees[$mangemployees[$i]][0][4]);
							
							$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('F'.$key,  $employees[$mangemployees[$i]][0][5]);

							
							$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->applyFromArray($styleArray4);
							$objPHPExcel->getActiveSheet()->setCellValue('G'.$key,  $employees[$mangemployees[$i]][0][6]);
							
							$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('H'.$key,  $employees[$mangemployees[$i]][0][7]);
							
							$objPHPExcel->getActiveSheet()->getStyle('I'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('I'.$key,  $employees[$mangemployees[$i]][0][8]);
							
							$objPHPExcel->getActiveSheet()->getStyle('J'.$key)->applyFromArray($styleArray3);
							$objPHPExcel->getActiveSheet()->setCellValue('J'.$key, $employees[$mangemployees[$i]][0][9]);
							
							$objPHPExcel->getActiveSheet()->getStyle('K'.$key)->applyFromArray($styleArray2);
							$objPHPExcel->getActiveSheet()->setCellValue('K'.$key, $employees[$mangemployees[$i]][0][10]);
							
							
							$date1 = $employees[$mangemployees[$i]][0][7];
						    $date2 = date("Y-m-d");
						
						    $diff = abs(strtotime($date2) - strtotime($date1));
						
						    $years = floor($diff / (365 * 60 * 60 * 24));
						    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
						    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

							
							$objPHPExcel->getActiveSheet()->getStyle('L'.$key)->applyFromArray($styleArray3);
							$objPHPExcel->getActiveSheet()->setCellValue('L'.$key,  $years.' '.$l_year.','.$months.' '. $l_month.','.$days.' '.$l_date);*/

		            		$key++;
           			
           		}
           
	include 'PHPExcel/IOFactory.php';
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Content-type: application/vnd.ms-excel");
	header('Content-Disposition: attachment; filename=' . $filename);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');

exit;

?>

